import axios from 'axios'
import {getUserByCookie} from './api/index'
import Cookies from 'js-cookie'

// 封装ajax 请求   成功返回数据或者失败返回失败信息
export const makePost = (url, param) => {
    return new Promise(async (res, rej) => {
        try {
            const resultT = await axios.post(url, param)
            let result = resultT.data;
            if (result.code == 0) {
                res(result.data)
            } else {
                rej(result.msg)
            }
        } catch (error) {
            rej(error.message)
        }
    })
}

export const autoLogin = (props) => {
    // 使用cookie自动登陆
    const userId = Cookies.get("user_id");
    if (userId) {
      getUserByCookie().then((result) => {
        props.dispatch({type: "Update_User_Info",data: result})
      }).catch((err) => {
        alert(err)
        props.history.push("/login")
      });
    }else{
        props.history.push("/login")
    }
}